//
//  AddCreditCardVC.m
//  ManageMoney
//
//  Created by 大明 on 16/4/21.
//  Copyright © 2016年 db. All rights reserved.
//

#import "AddCreditCardVC.h"

@interface AddCreditCardVC ()
{
    NSTimer *countDownTimer;
    NSInteger secondsCountDown; //60秒倒数计时
}
@property (weak, nonatomic) IBOutlet UITextField *nameTextfield;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *IDCardLabel;
@property (weak, nonatomic) IBOutlet UITextField *mobileTextfield;
@property (weak, nonatomic) IBOutlet UITextField *VerificationCodeTextfield;
@property (weak, nonatomic) IBOutlet UIButton *VerificationCodeBtn;

@end

@implementation AddCreditCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加信用卡";
    self.nameLabel.text = CheckNull(UserInfo[@"NAME"]);
    self.IDCardLabel.text = CheckNull(UserInfo[@"idCard"]);
}


//验证码
- (IBAction)VerificationCodeClick:(id)sender {
    [self.view endEditing:YES];
    
    if ([_nameTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"卡号不能为空"];
        return;
    }
    if ([_mobileTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return;
    }
    
    NSString * regex = @"(^1[0-9]{10}$)";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (!([pred evaluateWithObject:_mobileTextfield.text.trim])) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"phone":_mobileTextfield.text,@"appCode":@"B001"};
    
    
    [SVProgressHUD show];
    NSString *requestRegisterMessageUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/sendSm.do"];
    [HWHttpTool post:requestRegisterMessageUrl params:pramas success:^(id json) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            [SVProgressHUD showSuccessWithStatus:dic[@"responseMessage"]];

            //            //发送短信
            //禁用短信发送按钮，一分钟后再次开启
            secondsCountDown = 60;//60秒倒计时
            countDownTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
}

-(void)timeFireMethod{
    secondsCountDown--;
    if(secondsCountDown<0){
        [countDownTimer invalidate];
        [_VerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_VerificationCodeBtn setBackgroundColor:[UIColor blueColor]];
        [_VerificationCodeBtn setEnabled:YES];
    }else{
        [_VerificationCodeBtn setEnabled:NO];
        [_VerificationCodeBtn setBackgroundColor:[UIColor lightGrayColor]];
        if([SVProgressHUD getSystemVersion ]>=8){
            [_VerificationCodeBtn setTitle:[NSString stringWithFormat:@"%li秒后再获取",(long)secondsCountDown] forState:UIControlStateNormal];
        }else{
            [_VerificationCodeBtn setTitle:@"请稍后片刻……" forState:UIControlStateNormal];
        }
    }
}
//绑定
- (IBAction)bindingClick:(id)sender {
    
    if ([_nameTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"卡号不能为空"];
        return;
    }
    
    
    
    if ([_VerificationCodeTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空"];
        return;
    }
    if ([_mobileTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return;
    }
    
    NSString *RegisterMessageUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/bindCreditCard.do"];
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"customerNo":CheckNull(UserInfo[@"NUMBER"]),@"cardNo":_nameTextfield.text,@"smCode":_VerificationCodeTextfield.text,@"phone":_mobileTextfield.text,@"appCode":@"B001"};
    
    
    [HWHttpTool post:RegisterMessageUrl params:pramas success:^(id json){
        NSLog(@"%@",json);
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            [self leftBarButtonClick:nil];
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        [SVProgressHUD dismiss];
        
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
